package cn.edu.zafu.bigdata.llc;

public class Element {
    public static int getElement(int[] arr, int index) {
        if (index < 0 || index >= arr.length) {
            // 当索引超出范围时抛出ArrayIndexOutOfBoundsException
            throw new ArrayIndexOutOfBoundsException("索引超出数组范围: " + index);
        }
        return arr[index];
    }

    public static void main(String[] args) {
        try {
            // 调用getElement方法
            int[] arr = {10, 20, 30, 40, 50};
            int index = 5; // 这里设置一个超出范围的索引来模拟异常情况
            int element = getElement(arr, index);
            System.out.println("索引" + index + "的元素是: " + element);
        } catch (ArrayIndexOutOfBoundsException e) {
            // 捕获异常并输出错误信息
            System.out.println("错误: " + e.getMessage());
        }
    }
}
